package com.shcz.migu.api;


import com.shcz.migu.BuildConfig;
import com.shcz.migu.preference_config.PreferenceUtil;
import com.shcz.migu.utils.Constants;
import com.shcz.migu.utils.LogUtil;
import com.shcz.migu.utils.UserInforUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 调用后台的接口,架构网络层采用Retroft+Rxjava+gson
 */
public class APIService {

    private static final String TAG = "APIService";
    //    public static final String URL_HOST = "http://123.234.82.23";//服务器端口
    //get请求
//    public static final String API_SERVER_URL = "https://api.douban.com/v2/";
    public static final boolean ISTEST_NET = true;//测试环境为true 正式环境改为false

    public static final String API_SERVER_URL = ISTEST_NET ? "https://sandbox-api.wakkaa.com/1/" : "https://api.wakkaa.com/1/";

    public static final String MEDIA_BASE_URL = ISTEST_NET ? "http://sandbox.f1.cyjx.com/" : "http://f1.cyjx.com/";


    public static final String WEBSOCKET_BASE_URL = ISTEST_NET ? "ws://sandbox-liveim.wakkaa.com:8000/" : "ws://liveim.wakkaa.com/";

    public static final String SHOP_URL = API_SERVER_URL + "dashboard/app/shop_url";

    public static final String UPDATEVERSION_URL = API_SERVER_URL + "dashboard/app/check_update";

    public static boolean isDebug = false;

    public static boolean isDebug() {
        return isDebug;
    }

    public static void setIsDebug(boolean isDebug) {
        APIService.isDebug = isDebug;
    }
    //    2. 新增 /dashboard/app/check_update  检测版本更新
//    新增 /dashboard/app/shop_url  店铺地址


//    /**
//     * 基础地址
//     * 初始化 retroft
//     */
//    private static final Retrofit sRetrofit = new Retrofit.Builder()
//            .baseUrl(API_SERVER_URL)
//            .addConverterFactory(GsonConverterFactory.create())
//            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作为回调适配器
//            .build();

    private static Retrofit mRetrofit;

    public static Retrofit retrofit() {
        if (mRetrofit == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            if (BuildConfig.DEBUG) {
                // Log信息拦截器
                HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                //设置 Debug Log 模式
                builder.addInterceptor(loggingInterceptor);
            }
//            OkHttpClient okHttpClient = builder.build();
            mRetrofit = new Retrofit.Builder()
                    .baseUrl(API_SERVER_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                    .client(okHttpClient)
                    .client(genericClient())
                    .build();

        }
        return mRetrofit;
    }

    public static OkHttpClient genericClient() {

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        if (BuildConfig.DEBUG) {
            // Log信息拦截器
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            //设置 Debug Log 模式
            builder.addInterceptor(loggingInterceptor);
        }

        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
//                String sessionId = PreferenceUtil.getString(LocalConstants.SESSIONID, "");
                String sessionId = UserInforUtils.getSessionId();
                LogUtil.d("sessionId", "sessionId = " + sessionId);
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("x-api-version", "1.2")
                        .addHeader("x-mdi-sess", sessionId)
//                        .addHeader("x-usr-sess", userSession)
                        .addHeader("x-client", PreferenceUtil.getString(Constants.ANDROID_INFO, ""))//TODO android(6.0;Nexus5)
                        .removeHeader("User-Agent")
                        .addHeader("User-Agent", "android")
                        .addHeader("Content-type", "application/json")
                        .build();
                return chain.proceed(request);
            }

        });
        OkHttpClient okHttpClient = builder.build();
//        Log.d("header", "session = " + PreferenceUtil.getString(LocalConstants.SESSIONID, ""));

        return okHttpClient;
    }

    private static final RetrofitRequest apiManager = retrofit().create(RetrofitRequest.class);

//    public static Observable<PromotionListResp> postPromotionApplies(Map<String, String> map) {
//        return apiManager.postPromotionApplies(map);
//    }


}
